/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.trabajo.modelo;

import com.trabajo.modelo.entidad.Ventas;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author wilmer noe
 */
public class VentasDAO {
    private EntityManagerFactory emf;
    private EntityManager em;

    public VentasDAO() {
        emf= Persistence.createEntityManagerFactory("com.mycompany_mavenproject2_war_1.0-SNAPSHOTPU");
        em= emf.createEntityManager();
    }
    
    public boolean insertarVenta(Ventas v) {
        boolean res= false;
        try {
            em.getTransaction().begin();
            em.persist(v);
            em.getTransaction().commit();
            res= true;
        } catch (Exception ex) {
            em.getTransaction().rollback();
        }
        return res;
    }
    
    public Ventas buscarVenta(int idVenta) {
        Ventas v= em.find(Ventas.class, idVenta);
        return v;
    }
    
    public List<Ventas> getListaVentas() {
        Query q= em.createQuery("SELECT v FROM ventas v");
        return q.getResultList();
    }
    
    public boolean eliminarVenta(int idVenta) {
        boolean res= false;
        try {
            em.getTransaction().begin();
            Ventas v= em.find(Ventas.class, idVenta);
            if(v != null) {
                em.remove(v);
            }
            em.getTransaction().commit();
            res= true;
        } catch (Exception ex) {
            em.getTransaction().rollback();
        }
        return res;
    }
}